package com.liujit.pay.wx.domain.params.refunds;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.liujit.pay.wx.domain.MyPayInfo;
import com.liujit.pay.wx.domain.params.order.OrderAmount;
import com.liujit.pay.wx.domain.params.order.OrderDetail;
import com.liujit.pay.wx.domain.params.order.OrderGoodsDetail;
import com.liujit.pay.wx.domain.params.order.OrderSceneInfo;
import lombok.Builder;

import java.util.List;

/**
 * @description: 退款请求参数
 * @author: liujun
 * @create: 2021-02-19 16:39
 */
@Builder
public class RefundsParam extends MyPayInfo {

    /**
     * 微信支付订单号
     */
    private String transactionId;

    /**
     * 商户订单号
     */
    private String outTradeNo;

    /**
     * 商户退款单号
     */
    private String outRefundNo;

    /**
     * 退款原因
     */
    private String reason;

    /**
     * 退款结果回调url
     */
    private String notifyUrl;

    /**
     * 退款资金来源
     */
    private String fundsAccount;

    /**
     * 金额信息
     */
    private RefundsAmount refundsAmount;

    /**
     * 退款商品
     */
    private List<RefundsGoodsDetail> refundsGoodsDetailList;

    RefundsParam(String transactionId, String outTradeNo, String outRefundNo, String reason, String notifyUrl, String fundsAccount,
                 RefundsAmount refundsAmount, List<RefundsGoodsDetail> refundsGoodsDetailList) {
        this.transactionId = transactionId;
        this.outTradeNo = outTradeNo;
        this.outRefundNo = outRefundNo;
        this.reason = reason;
        this.notifyUrl = notifyUrl;
        this.fundsAccount = fundsAccount;
        this.refundsAmount = refundsAmount;
        this.refundsGoodsDetailList = refundsGoodsDetailList;
    }

    /**
     * 初始化参数
     *
     * @param transactionId 微信支付订单号
     * @param outTradeNo    商户订单号
     * @param outRefundNo   商户退款单号
     * @param refundsAmount 金额信息
     * @return
     */
    public static RefundsParam.Builder builder(String transactionId, String outTradeNo, String outRefundNo, RefundsAmount refundsAmount) {
        return new RefundsParam.Builder(transactionId, outTradeNo, outRefundNo, refundsAmount);
    }

    public static class Builder {

        private String transactionId;
        private String outTradeNo;
        private String outRefundNo;
        private String reason;
        private String notifyUrl;
        private String fundsAccount;
        private RefundsAmount refundsAmount;
        private List<RefundsGoodsDetail> refundsGoodsDetailList;

        Builder(String transactionId, String outTradeNo, String outRefundNo, RefundsAmount refundsAmount) {

            this.transactionId = transactionId;
            this.outTradeNo = outTradeNo;
            this.outRefundNo = outRefundNo;
            this.refundsAmount = refundsAmount;
        }

        /**
         * 设置退款原因
         *
         * @param reason 退款原因
         * @return
         */
        public RefundsParam.Builder reason(String reason) {
            this.reason = reason;
            return this;
        }

        /**
         * 设置退款结果回调url
         *
         * @param notifyUrl 退款结果回调url
         * @return
         */
        public RefundsParam.Builder notifyUrl(String notifyUrl) {
            this.notifyUrl = notifyUrl;
            return this;
        }

        /**
         * 退款资金来源
         *
         * @param fundsAccount
         * @return
         */
        public RefundsParam.Builder fundsAccount(String fundsAccount) {
            this.fundsAccount = fundsAccount;
            return this;
        }

        /**
         * 设置退款商品
         *
         * @param refundsGoodsDetailList 退款商品
         * @return
         */
        public RefundsParam.Builder refundsGoodsDetailList(List<RefundsGoodsDetail> refundsGoodsDetailList) {
            this.refundsGoodsDetailList = refundsGoodsDetailList;
            return this;
        }

        public RefundsParam build() {
            return new RefundsParam(this.transactionId, this.outTradeNo, this.outRefundNo, this.reason, this.notifyUrl,
                    this.fundsAccount, this.refundsAmount, this.refundsGoodsDetailList);
        }
    }

    /**
     * 转化JSON字符串
     *
     * @return
     */
    public String toJSONString() {
        JSONObject json = new JSONObject();
        // 微信支付订单号
        json.set("transaction_id", transactionId);
        // 商户订单号
        json.set("out_trade_no", outTradeNo);
        // 商户退款单号
        json.set("out_refund_no", outRefundNo);
        // 退款原因
        if (StrUtil.isNotBlank(reason)) {
            json.set("reason", reason);
        }
        // 退款结果回调url
        if (StrUtil.isNotBlank(notifyUrl)) {
            json.set("notify_url", notifyUrl);
        }
        // 退款资金来源
        if (StrUtil.isNotBlank(fundsAccount)) {
            json.set("funds_account", fundsAccount);
        }
        json.set("amount", refundsAmount.toJSONString());
        // 退款商品
        if (refundsGoodsDetailList != null && refundsGoodsDetailList.size() > 0) {
            JSONArray array = new JSONArray();
            for (RefundsGoodsDetail detail : refundsGoodsDetailList) {
                array.set(detail.toJSONObject());
            }
            json.set("goods_detail", array.toString());
        }
        return json.toString();
    }
}
